package cn.edu.scau.cmi.yangminghao.project;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import cn.edu.scau.cmi.yangminghao.target.Student;
import cn.edu.scau.cmi.yangminghao.target.Tutor;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class MyController {
	@FXML
	private Label label1;
	@FXML
	private Label label2;
	@FXML
	private TableView tableview1;
	@FXML
	private TableColumn tbc11;
	@FXML
	private TableColumn tbc12;
	@FXML
	private TableColumn tbc13;
	@FXML
	private TableView tableview2;
	@FXML
	private TableColumn tbc21;
	@FXML
	private TableColumn tbc22;
	@FXML
	private TextField tf11;
	@FXML
	private TextField tf12;
	@FXML
	private TextField tf13;
	@FXML
	private Button bt1;
	@FXML
	private ChoiceBox cb1;
	@FXML
	private ChoiceBox cb2;
	@FXML
	private TextField tf21;
	@FXML
	private TextField tf22;
	@FXML
	private TextField tf23;
	@FXML
	private Button bt2;
	@FXML
	private Button bt3;
	@FXML
	private Button bt6;
	@FXML
	private Button bt5;
	@FXML
	private TextField tf42;
	@FXML
	private TextField tf41;
	@FXML
	private ChoiceBox cb4;
	@FXML
	private ChoiceBox cb3;
	@FXML
	private Button bt4;
	@FXML
	private Button bt7;
	@FXML
	private TextField tf32;
	@FXML
	private TextField tf31;
	boolean start = false;
	private List<Student> list1; // 存储发送窗口的数据
	private List<Tutor> list2; // 存储接收窗口的数据
	public static int colu1;
	public static int colu2;
	public static int colu3;
	public static int colu4;
	static ObservableList<Student> data1 = FXCollections.observableArrayList( // 数据集1
	);
	static ObservableList<Tutor> data2 = FXCollections.observableArrayList( // 数据集2
	);

	public void firstRun() {// 用于填充list1

		SessionFactory sf = new Configuration().configure().buildSessionFactory();

		Session session = sf.openSession(); // 生成一个session

		session.beginTransaction(); // 开启事务

		Query query = session.createSQLQuery("select * from student").addEntity(Student.class);

//        String hql = "select *from product01";
//
//        Query query = session.createQuery(hql);

		list1 = query.list();

		query = session.createSQLQuery("select * from tutor").addEntity(Tutor.class);

//      String hql = "select *from product01";
//
//      Query query = session.createQuery(hql);

		list2 = query.list();

		session.getTransaction().commit(); // 提交事务

		session.close(); // 关闭session

		sf.close();

		update();

	}

	@FXML
	public void button1() {

		SessionFactory sf = new Configuration().configure().buildSessionFactory();

		Session s = sf.openSession();
		s.beginTransaction();

		Student p = new Student();

		p.setId(tf11.getText());
		p.setName(tf12.getText());
		p.setTutor_id(tf13.getText());
		s.save(p);

		s.getTransaction().commit();
		s.close();
		sf.close();
		button7();
	}

	@FXML
	public void button4() {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();

		Session s = sf.openSession();
		s.beginTransaction();

		Tutor p = new Tutor();

		p.setId(tf31.getText());
		p.setName(tf32.getText());

		s.save(p);

		s.getTransaction().commit();
		s.close();
		sf.close();
		button7();
	}

	public void update() {
		data1.clear();
		data2.clear();

		for (Student ss : list1)
			data1.add(ss);
		tbc11.setCellValueFactory(new PropertyValueFactory<>("id"));
		tbc12.setCellValueFactory(new PropertyValueFactory<>("name"));
		tbc13.setCellValueFactory(new PropertyValueFactory<>("tutor_id"));
		tableview1.setItems(data1);

		// tableview2

		for (Tutor ss : list2)
			data2.add(ss);
		tbc21.setCellValueFactory(new PropertyValueFactory<>("id"));
		tbc22.setCellValueFactory(new PropertyValueFactory<>("name"));
		tableview2.setItems(data2);
	}

	@FXML
	public void button7() {
		
		firstRun();
		ObservableList<String> data3 = FXCollections.observableArrayList(	//建立数据集	
				);
		///////////////////////////////////////////////////////////////
		for (Student ss:list1)//插入choicebox中
			data3.add(ss.getId());
		cb1.setItems(FXCollections.observableArrayList(data3));
		cb2.setItems(FXCollections.observableArrayList(data3));
		if(start==false) {
			cb1.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener() {
				@Override
				public void changed(ObservableValue observable, Object oldValue, Object newValue) {
					// TODO Auto-generated method stub
					colu3=(int)newValue;
					
					int tem=(int)newValue;
					if((int)newValue==-1)
						tem=0;
					
							
					tf21.setText(list1.get(tem).getId());
					tf22.setText(list1.get(tem).getName());
					tf23.setText(list1.get(tem).getTutor_id());
					
					
				}
			});
			cb2.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener() {
				@Override
				public void changed(ObservableValue observable, Object oldValue, Object newValue) {
					// TODO Auto-generated method stub
					
					colu1=(int)newValue;
					
				}
			});
		}
	
		
		///////////////////////////////////////////////////////////////
		ObservableList<String> data4 = FXCollections.observableArrayList(	//建立数据集	
				);
		for (Tutor ss:list2)//插入choicebox中
			data4.add(ss.getId());
		cb3.setItems(FXCollections.observableArrayList(data4));
		cb4.setItems(FXCollections.observableArrayList(data4));
		if(start==false) {
			cb3.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener() {
				@Override
				public void changed(ObservableValue observable, Object oldValue, Object newValue) {
					// TODO Auto-generated method stub
					colu4=(int)newValue;
					int tem=(int)newValue;
					if((int)newValue==-1)
						tem=0;
					tf41.setText(list2.get(tem).getId());
					tf42.setText(list2.get(tem).getName());
					
				}
			});
			cb4.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener() {
				@Override
				public void changed(ObservableValue observable, Object oldValue, Object newValue) {
					// TODO Auto-generated method stub
					System.out.println((int)newValue);
					colu2=(int)newValue;
				}
			});
			start=true;
		}
		
	}

	@FXML
	public void button3() {

		SessionFactory sf = new Configuration().configure().buildSessionFactory();

		Session s = sf.openSession();
		s.beginTransaction();
		s.delete(list1.get(colu1));
		s.getTransaction().commit();
		s.close();
		sf.close();

		button7();

	}

	@FXML
	public void button6() {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();

		Session s = sf.openSession();
		s.beginTransaction();
		s.delete(list2.get(colu2));
		s.getTransaction().commit();
		s.close();
		sf.close();

		button7();
	}

	public void excute() {
		firstRun();

	}

	@FXML
	public void button2() {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();

		Session s = sf.openSession();
		s.beginTransaction();
		Student product = (Student) s.get(Student.class, list1.get(colu3).getId());

		product.setName(tf22.getText());
		product.setTutor_id(tf23.getText());

		s.save(product);
		s.getTransaction().commit();
		s.close();
		sf.close();

		button7();
	}

	@FXML
	public void button5() {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();

		Session s = sf.openSession();
		s.beginTransaction();
		Tutor product = (Tutor) s.get(Tutor.class, list2.get(colu4).getId());

		product.setName(tf42.getText());

		s.save(product);
		s.getTransaction().commit();
		s.close();
		sf.close();

		button7();
	}
}